﻿using Sprout.Core.Extensions;
using System;
using System.Collections.Generic;
using System.Text;

namespace Sprout.Core.Utils
{
    public class EnumHelper
    {
        /// <summary>
        /// 获取枚举的所有值、说明 集合
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <returns></returns>
        public static IEnumerable<KeyValuePair<int, string>> GetForDescription<T>()
        {
            if (typeof(T).IsEnum)
            {
                List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
                foreach (System.Enum item in System.Enum.GetValues(typeof(T)))
                {

                    list.Add(new KeyValuePair<int, string>(Convert.ToInt32(item), item.ToDescription()));
                }
                return list;
            }
            else
            {
                throw new Exception("泛型只能是枚举类型");
                //return new List<KeyValuePair<int, string>>();
            }
        }
        /// <summary>
        /// 获取枚举的所有值、Key 集合
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <returns></returns>
        public static IEnumerable<KeyValuePair<int, string>> GetForKey<T>()
        {
            if (typeof(T).IsEnum)
            {
                List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
                foreach (System.Enum item in System.Enum.GetValues(typeof(T)))
                {

                    list.Add(new KeyValuePair<int, string>(Convert.ToInt32(item), item.ToString()));
                }
                return list;
            }
            else
            {
                throw new Exception("泛型只能是枚举类型");
                //return new List<KeyValuePair<int, string>>();
            }
        }
    }
}
